-
Notifications
You must be signed in to change notification settings - Fork 2.9k
chore: use Cow in scalars/HexString #3034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
|
||
| #[derive(Clone, Debug, derive_more::Into, derive_more::From, PartialEq, Eq)] | ||
| pub struct HexString(pub(crate) Vec<u8>); | ||
| pub struct HexString(pub(crate) Cow<'static, [u8]>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we want a static lifetime here, can we use
| pub struct HexString(pub(crate) Cow<'static, [u8]>); | |
| pub struct HexString<'a>(pub(crate) Cow<'a, [u8]>); |
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rymnc I've changed implementation to use 'a instead of 'static. Please review.
7a9aec6 to
a571b5f
Compare
|
@rymnc I applied the suggested changes. Please review. |
5dcdbf0 to
06150af
Compare
06150af to
ae7848d
Compare
| // TODO: Avoid cloning the bytes here. | ||
| // https://github.com/FuelLabs/fuel-core/issues/2657 | ||
| HexString(bytes.as_ref().clone()) | ||
| HexString::from(bytes.as_ref().clone()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cow::Owned is still needed here and as a result - cloning. Because Cow::Borrowed cannot be returned since it references only to a data that is owned by this map. Here's a compilation failing example if we use Cow::Borrowed:
error[E0515]: cannot return value referencing function parameter `bytes`
--> crates/fuel-core/src/schema/block.rs:443:21
|
443 | HexString::from(bytes.as_ref().as_slice())
| ^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^
| | |
| | `bytes` is borrowed here
| returns a value referencing data owned by the current function
|
hey @rymnc please check |
Linked Issues/PRs
close #2657
Description
Checklist
Before requesting review
After merging, notify other teams
[Add or remove entries as needed]
Note
Replaces owned HexString with Cow-backed HexString<'_> and updates GraphQL types/methods to borrow bytes where possible, reducing clones and adding required lifetimes.
HexStringwithHexString<'a>backed byCow<[u8]>.From<Vec<u8>>,From<&[u8]>,From<HexString> for Vec<u8>; updateDisplay,ScalarType,CursorType,FromStr, andNonceconversions to support borrowing.HexStringtoHexString<'_>across schema:blob,contract,da_compressed,message,storage,tx(queries, mutations, subscriptions, types, receipts), andupgrades.Input<'a>,InputCoin<'a>,InputMessage<'a>,UploadedBytecode<'a>, subscriptions).HexStringvia borrowing (HexString::from(&[..])/.into()) and remove unnecessary clones in mappers (e.g., witnesses, scripts, predicate/data, storage slots, block/tx streams).HexString<'_>where relevant.Written by Cursor Bugbot for commit ae7848d. This will update automatically on new commits. Configure here.